
package com.iotp.controller.admin.menu;

import com.alibaba.fastjson.JSONObject;
import com.iotp.controller.admin.base.BaseController;
import com.iotp.entity.base.DataMessage;
import com.iotp.entity.base.KKpager;
import com.iotp.entity.menu.Menu;
import com.iotp.service.menu.MenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iotp.utils.common.WyConstant;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import java.io.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统菜单管理控制器
 * @author Assassin
 */
@Controller
@RequestMapping("/sys/menu")
public class MenuController extends BaseController {
	
	// Fields
	
	@Autowired
	private MenuService menuService;// 系统菜单业务接口
	
	/**
	 * 进入系统菜单列表页面
	 * @return sys/menu/list_menu
	 */
	@GetMapping("/list")
	public String gotoListPage() {
		return "admin/sys/menu/list_menu";
	}
	
	/**
	 * 进入系统根菜单添加页面
	 * @return sys/menu/add_root_menu
	 */
	@GetMapping("/toAddRoot")
	public String gotoAddRootPage() {
		return "admin/sys/menu/add_root_menu";
	}
	
	/**
	 * 进入系统子菜单添加页面
	 * @param menuPid 父级菜单Id
	 * @return sys/menu/add_sub_menu
	 */
	@GetMapping("/toAddSub")
	public String gotoAddSubPage(@RequestParam(required = true, value = "menuPid") String menuPid) {
		// 后台数据验证
		if(!this.validateStringProperty(menuPid, 50, true)) {
			// TODO 错误页面
			return "redirect:/error";
		} 
		Menu parentMenu = this.menuService.queryById(Long.parseLong(menuPid));
		if(parentMenu == null) {
			return "redirect:/error";
		}
		request.setAttribute("menuPid", menuPid);
		return "admin/sys/menu/add_sub_menu";
	}
	
	/**
	 * 进入系统菜单编辑页面
	 * @param menuId 菜单Id
	 * @return sys/menu/edit_menu
	 */
	@GetMapping("/toEdit")
	public String gotoEditPage(@RequestParam(required = true, value = "menuId") String menuId) {
		// 后台数据验证
		if(!this.validateStringProperty(menuId, 50, true)) {
			// TODO 错误页面
			return "redirect:/error";
		} 
		Menu editMenu = this.menuService.queryById(Long.parseLong(menuId));
		if(editMenu == null) {
			return "redirect:/error";
		}
		request.setAttribute("editMenu", editMenu);
		return "admin/sys/menu/edit_menu";
	}
	
	/**
	 * 进入系统菜单图标列表页面
	 * @return sys/menu/select_icon
	 */
	@GetMapping("/icon")
	public String gotoSelectIconPage() {
		return "admin/sys/menu/select_icon";
	}
	
	/**
	 * 加载菜单树表格数据
	 * @param pageNo 当前页码
	 * @param pageSize 每页记录数
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/treeTable")
	public DataMessage selectTreeTable(@RequestParam(required = false, value = "pageNo") Integer pageNo,
			@RequestParam(required = false, value = "pageSize") Integer pageSize) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		List<Menu> resultList = new ArrayList<Menu>();
		PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
		List<Menu> menuList = this.menuService.queryList(null, null, getOrgType(), getOrgId(), null, 0, null, 1,
				null, null, null, null, null, null, null, null, null, null);
		PageInfo<Menu> pageInfo = new PageInfo<Menu>(menuList);
		for(Menu menu : menuList) {
			this.recursionSubMenu(resultList, menu);
		}
		dm.setResult(WyConstant.RESULT_CODE_SUCCESS);
		dm.setData(resultList);
		KKpager pager = new KKpager(getPageNo(pageNo), pageInfo.getPages() , pageInfo.getTotal());
		dm.setMessage(JSONObject.toJSON(pager).toString());
		return dm;
	}
	
	/**
	 * 验证权限是否存在
	 * @param menuId 菜单Id
	 * @param menuName 菜单名称
	 * @param menuUrl 菜单地址
	 * @param -validateType[menuName | menuUrl]
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/validateIsMenuExist")
	public DataMessage validateIsMenuExist(
			@RequestParam(required = false, value = "menuId") String menuId,
			@RequestParam(required = false, value = "menuName") String menuName,
			@RequestParam(required = false, value = "menuUrl") String menuUrl,
			@RequestParam(required = true, value = "validateType") String validateType) throws ParseException {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(validateType, 50, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("验证类型参数错误");
		} else if(!this.validateStringProperty(menuId, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单标识参数错误");
		} else if(!this.validateStringProperty(menuName, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单名称参数错误");
		} else if(!this.validateStringProperty(menuUrl, 255, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单URL参数错误");
		} else {
			Long mid = null;
			if(menuId != null && !"".equals(menuId.trim())) {
				mid = Long.parseLong(menuId);
			}
			dm.setResult(WyConstant.RESULT_CODE_SUCCESS);
			List<Menu> menuList = this.menuService.queryIsMenuExist(mid, menuName, menuUrl);
			if(menuList == null || menuList.size() == 0) {
				dm.setData("0");
			} else {
				dm.setData("1");
			}
		}
		return dm;
	}
	
	/**
	 * 添加系统菜单根菜单
	 * @param menuName 菜单名称
	 * @param aliasName 菜单别名
	 * @param menuIcon 菜单图标
	 * @param menuDesc 菜单描述
	 * @param menuUrl 菜单URL
	 * @param authPerms 菜单授权
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/addRoot")
	public DataMessage addRoot(
			@RequestParam(required = true, value = "menuName") String menuName,
			@RequestParam(required = false, value = "aliasName") String aliasName,
			@RequestParam(required = false, value = "menuIcon") String menuIcon,
			@RequestParam(required = false, value = "menuDesc") String menuDesc,
			@RequestParam(required = true, value = "menuUrl") String menuUrl,
			@RequestParam(required = true, value = "authPerms") String authPerms) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(menuName, 50, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单名称参数错误");
		} else if(!this.validateStringProperty(aliasName, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单别名参数错误");
		} else if(!this.validateStringProperty(menuIcon, 128, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单图标参数错误");
		} else if(!this.validateStringProperty(menuDesc, 255, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单说明参数错误");
		} else if(!this.validateStringProperty(menuUrl, 255, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单URL参数错误");
		} else if(!this.validateStringProperty(authPerms, 500, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单授权参数错误");
		} else {
			boolean isExist = false;
			List<Menu> menuList = this.menuService.queryIsMenuExist(null, menuName, null);
			if(menuList != null && menuList.size() > 0) {
				dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
				dm.setMessage("菜单名称已经存在");
				isExist = true;
				return dm;
			} 
			menuList = this.menuService.queryIsMenuExist(null, null, menuUrl);
			if(menuList != null && menuList.size() > 0) {
				dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
				dm.setMessage("菜单URL已经存在");
				isExist = true;
				return dm;
			} 
			if(!isExist) {
				Long menuId = menuService.addRoot(getOrgType(), getOrgId(), menuName, aliasName, menuIcon, menuDesc, menuUrl, authPerms);
				dm.setResult(WyConstant.RESULT_CODE_SUCCESS);
				dm.setData(menuId);
				dm.setMessage("根菜单添加成功");
			}
		}
		return dm;
	}
	
	/**
	 * 添加系统菜单子菜单
	 * @param menuPid 父级Id
	 * @param menuName 菜单名称
	 * @param aliasName 菜单别名
	 * @param menuIcon 菜单图标
	 * @param menuDesc 菜单描述
	 * @param menuUrl 菜单URL
	 * @param authPerms 菜单授权
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/addSub")
	public DataMessage addSub(
			@RequestParam(required = true, value = "menuPid") String menuPid,
			@RequestParam(required = true, value = "menuName") String menuName,
			@RequestParam(required = false, value = "aliasName") String aliasName,
			@RequestParam(required = false, value = "menuIcon") String menuIcon,
			@RequestParam(required = false, value = "menuDesc") String menuDesc,
			@RequestParam(required = true, value = "menuUrl") String menuUrl,
			@RequestParam(required = true, value = "authPerms") String authPerms) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(menuPid, 50, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单父级别参数错误");
		} else if(!this.validateStringProperty(menuName, 50, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单名称参数错误");
		} else if(!this.validateStringProperty(aliasName, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单别名参数错误");
		} else if(!this.validateStringProperty(menuIcon, 128, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单图标参数错误");
		} else if(!this.validateStringProperty(menuDesc, 255, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单说明参数错误");
		} else if(!this.validateStringProperty(menuUrl, 255, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单URL参数错误");
		} else if(!this.validateStringProperty(authPerms, 500, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单授权参数错误");
		} else {
			boolean isExist = false;
			List<Menu> menuList = this.menuService.queryIsMenuExist(null, menuName, null);
			if(menuList != null && menuList.size() > 0) {
				dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
				dm.setMessage("菜单名称已经存在");
				isExist = true;
				return dm;
			} 
			menuList = this.menuService.queryIsMenuExist(null, null, menuUrl);
			if(menuList != null && menuList.size() > 0) {
				dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
				dm.setMessage("菜单URL已经存在");
				isExist = true;
				return dm;
			} 
			if(!isExist) {
				Long menuId = menuService.addSub(Long.parseLong(menuPid), menuName, aliasName, menuIcon, menuDesc, menuUrl, authPerms);
				dm.setResult(WyConstant.RESULT_CODE_SUCCESS);
				dm.setData(menuId);
				dm.setMessage("子菜单添加成功");
			}
		}
		return dm;
	}
	
	/**
	 * 编辑系统菜单
	 * @param menuId 菜单Id
	 * @param menuName 菜单名称
	 * @param aliasName 菜单别名
	 * @param menuIcon 菜单图标
	 * @param menuDesc 菜单描述
	 * @param menuUrl 菜单URL
	 * @param authPerms 菜单授权
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/edit")
	public DataMessage edit(
			@RequestParam(required = true, value = "menuId") String menuId,
			@RequestParam(required = true, value = "menuName") String menuName,
			@RequestParam(required = false, value = "aliasName") String aliasName,
			@RequestParam(required = false, value = "menuIcon") String menuIcon,
			@RequestParam(required = false, value = "menuDesc") String menuDesc,
			@RequestParam(required = true, value = "menuUrl") String menuUrl,
			@RequestParam(required = true, value = "authPerms") String authPerms) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(menuId, 50, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单Id参数错误");
		} else if(!this.validateStringProperty(menuName, 50, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单名称参数错误");
		} else if(!this.validateStringProperty(aliasName, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单别名参数错误");
		} else if(!this.validateStringProperty(menuIcon, 128, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单图标参数错误");
		} else if(!this.validateStringProperty(menuDesc, 255, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单说明参数错误");
		} else if(!this.validateStringProperty(menuUrl, 255, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单URL参数错误");
		} else if(!this.validateStringProperty(authPerms, 500, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单授权参数错误");
		} else {
			boolean isExist = false;
			List<Menu> menuList = this.menuService.queryIsMenuExist(Long.parseLong(menuId), menuName, null);
			if(menuList != null && menuList.size() > 0) {
				dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
				dm.setMessage("菜单名称已经存在");
				isExist = true;
				return dm;
			} 
			menuList = this.menuService.queryIsMenuExist(Long.parseLong(menuId), null, menuUrl);
			if(menuList != null && menuList.size() > 0) {
				dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
				dm.setMessage("菜单URL已经存在");
				isExist = true;
				return dm;
			} 
			if(!isExist) {
				this.menuService.edit(Long.parseLong(menuId), null, null, null, menuName, aliasName, menuIcon, menuDesc, menuUrl, authPerms);
				dm.setResult(WyConstant.RESULT_CODE_SUCCESS);
				dm.setMessage("菜单编辑成功");
			}
		}
		return dm;
	}
	
	/**
	 * 删除系统菜单
	 * @param menuId 菜单Id
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/delete")
	public DataMessage delete(@RequestParam(required = true, value = "menuId") String menuId) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(menuId, 50, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单Id参数错误");
		} else {
			menuService.editIsDel(Long.parseLong(menuId), 1);
			dm.setResult(WyConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("菜单删除成功");
		}
		return dm;
	}
	
	/**
	 * 批量删除系统菜单
	 * @param menuIds 菜单Ids
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/batchDelete")
	public DataMessage deleteBatch(@RequestParam(required = true, value = "menuIds") String menuIds) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(menuIds, 128, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单Ids参数错误");
		} else {
			String[] menuIdsArr = menuIds.split(",");
			for(String menuId : menuIdsArr) {
				menuService.editIsDel(Long.parseLong(menuId), 1);
			}
			dm.setResult(WyConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("菜单删除成功");
		}
		return dm;
	}
	
	/**
	 * 导出数据到Excel
	 * @return Excel File
	 * @throws Exception
	 */
	@PostMapping("/export")
    public ModelAndView exportExcel() throws Exception {
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("系统菜单信息");
        // 手动设置列宽。第一个参数表示要为第几列设；，第二个参数表示列的宽度，n为列高的像素数。
        sheet.setColumnWidth((short) 0, (short) (15.7 * 150));
        sheet.setColumnWidth((short) 1, (short) (25.7 * 150));
        sheet.setColumnWidth((short) 2, (short) (15.7 * 150));
        sheet.setColumnWidth((short) 3, (short) (35.7 * 150));
        sheet.setColumnWidth((short) 4, (short) (35.7 * 150));
        sheet.setColumnWidth((short) 5, (short) (35.7 * 150));
        sheet.setColumnWidth((short) 6, (short) (35.7 * 150));
        sheet.setColumnWidth((short) 7, (short) (35.7 * 150));
        sheet.setColumnWidth((short) 8, (short) (35.7 * 150));
        // 创建第一行
        Row row = sheet.createRow((short) 0);
        // 创建两种单元格格式
        CellStyle cs = wb.createCellStyle();
        CellStyle cs2 = wb.createCellStyle();
        
        // 创建两种字体
        Font f = wb.createFont();
        Font f2 = wb.createFont();
        
        // 创建第一种字体样式
        f.setFontHeightInPoints((short) 12);
        f.setColor(IndexedColors.BLACK.getIndex());
        f.setBoldweight(Font.BOLDWEIGHT_BOLD);
        // 创建第二种字体样式
        f2.setFontHeightInPoints((short) 11);
        f2.setColor(IndexedColors.BLACK.getIndex());
        f2.setBoldweight(Font.BOLDWEIGHT_NORMAL);
        
        // 设置第一种单元格的样式
        cs.setFont(f);
        cs.setBorderLeft(CellStyle.BORDER_THIN);
        cs.setBorderRight(CellStyle.BORDER_THIN);
        cs.setBorderTop(CellStyle.BORDER_THIN);
        cs.setBorderBottom(CellStyle.BORDER_THIN);
        cs.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        
        // 设置第二种单元格的样式
        cs2.setFont(f2);
        cs2.setBorderLeft(CellStyle.BORDER_THIN);
        cs2.setBorderRight(CellStyle.BORDER_THIN);
        cs2.setBorderTop(CellStyle.BORDER_THIN);
        cs2.setBorderBottom(CellStyle.BORDER_THIN);
        cs2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        
        // 创建列（每行里的单元格）
        Cell cell = row.createCell(0);
        cell.setCellValue("序号");
        cell.setCellStyle(cs);
        
        cell = row.createCell(1);
        cell.setCellValue("菜单编码");
        cell.setCellStyle(cs);
        
        cell = row.createCell(2);
        cell.setCellValue("菜单级别");
        cell.setCellStyle(cs);
        
        cell = row.createCell(3);
        cell.setCellValue("菜单名称");
        cell.setCellStyle(cs);
        
        cell = row.createCell(4);
        cell.setCellValue("菜单别名");
        cell.setCellStyle(cs);
        
        cell = row.createCell(5);
        cell.setCellValue("菜单图标");
        cell.setCellStyle(cs);
        
        cell = row.createCell(6);
        cell.setCellValue("菜单说明");
        cell.setCellStyle(cs);
        
        cell = row.createCell(7);
        cell.setCellValue("菜单URL");
        cell.setCellStyle(cs);
        
        cell = row.createCell(8);
        cell.setCellValue("菜单授权");
        cell.setCellStyle(cs);
        
        PageHelper.orderBy("menu_code");
    	List<Menu> menuList= this.menuService.queryList(null, null, getOrgType(), getOrgId(), null, 0, null, null, null, null, null, null, null, null, null, null, null, null);
    	for(int i = 0; i < menuList.size(); i++) {
    		Menu menu = menuList.get(i);
    		row = sheet.createRow(i + 1);
    		cell = row.createCell(0);
    		cell.setCellValue(i + 1);
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(1);
    		cell.setCellValue(menu.getMenuCode());
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(2);
    		cell.setCellValue(menu.getMenuLevel());
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(3);
    		cell.setCellValue(menu.getMenuName());
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(4);
    		cell.setCellValue((menu.getAliasName() == null || "".equals(menu.getAliasName().trim())) ? "—" : menu.getAliasName());
    	    cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(5);
    		cell.setCellValue((menu.getMenuIcon() == null || "".equals(menu.getMenuIcon().trim())) ? "—" : menu.getMenuIcon());
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(6);
    		cell.setCellValue((menu.getMenuDesc() == null || "".equals(menu.getMenuDesc().trim())) ? "—" : menu.getMenuDesc());
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(7);
    		cell.setCellValue(menu.getMenuUrl());
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(8);
    		cell.setCellValue(menu.getAuthPerms());
    		cell.setCellStyle(cs2);
    	}
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            wb.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] content = os.toByteArray();
        InputStream is = new ByteArrayInputStream(content);
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(("系统菜单信息" + ".xls").getBytes(), "ISO-8859-1"));
        ServletOutputStream out = response.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (final IOException e) {
            throw e;
        } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
        return null;
	}
	
	/**
	 * 递归添加菜单子集
	 * @param resultList 结果List
	 * @param parentMenu 父级菜单
	 */
	private void recursionSubMenu(List<Menu> resultList, Menu parentMenu) {
		if(parentMenu.getIsParent().intValue() == 1) {
			List<Menu> subList = this.menuService.querySubList(parentMenu.getMenuId());
			parentMenu.setSubList(subList);
			resultList.add(parentMenu);
			for(Menu sub : subList) {
				if(sub.getIsParent().intValue() == 1) {
					recursionSubMenu(resultList, sub);
				}
			}
		} else {
			resultList.add(parentMenu);
		}
	}
	
}